<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>自定义计数器组件</title>
</head>
<body>
<div id="app">
    <counter v-bind:count="initial_count" v-on:changed="countChanged"></counter>
</div>

<template id="cpn">
    <h1>{{ count }}</h1>
    <button v-on:click="decrease">-</button>
    <button v-on:click="increase">+</button>
</template>
<script type="module">
    import {createApp, ref, defineComponent} from "../../../js/vue.esm-browser.js";

    let component = defineComponent(
        {
            template: "#cpn",
            props: {
                count: {
                    type: Number,
                    default: 0,
                    required: true,
                }
            },
            emits: ['changed'],
            setup(props, context) {
                let count = ref(props.count)

                function decrease() {
                    count.value -= 1
                    context.emit('changed', count.value)
                }

                function increase() {
                    count.value += 1
                    context.emit('changed', count.value)
                }

                return {
                    count, decrease, increase,
                };
            }
        }
    );

    let app = createApp(
        {
            setup() {
                let initial_count = ref(100)

                function countChanged(event) {
                    console.log(event);
                }

                return {
                    initial_count, countChanged,
                }
            }
        }
    );

    app.component("counter", component)
    app.mount("#app")
</script>
</body>
</html>